<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="database" scope="request" class="accessibleworldsdatabase.AccessibleWorldsDatabaseJavaBeans"/>
<jsp:useBean id="des" scope="request" class="accessibleworldsdes.AccessibleWorldsDESJavaBeans"/>
<jsp:useBean id="mail" scope="request" class="accessibleworldsmail.AccessibleWorldsMailJavaBeans"/>
<%try {

String accessCode = request.getParameter("accessCode");

if(!(accessCode==null || "".equals(accessCode))) {
	
    accessCode = des.decryptString(session.getId(), accessCode);
    accessCode = URLDecoder.decode(accessCode, "UTF-8");
    
    if(des.getAndroidAccessCode().equals(accessCode) || des.getComputerAccessCode().equals(accessCode)) {
    	
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String lastName = request.getParameter("lastName");
        String firstName = request.getParameter("firstName");
        String birthday = request.getParameter("birthday");
        
		if(!(email==null ||"".equals(email) || password==null ||"".equals(password) ||
			lastName==null || "".equals(lastName) || firstName==null ||"".equals(firstName) ||
         	birthday==null || "".equals(birthday))) {
            
            email = des.decryptString(session.getId(), email);
            password = des.decryptString(session.getId(), password);
            lastName = des.decryptString(session.getId(), lastName);
            firstName = des.decryptString(session.getId(), firstName);
            birthday = des.decryptString(session.getId(), birthday);
            
            email = URLDecoder.decode(email, "UTF-8");
            password = URLDecoder.decode(password, "UTF-8");
            lastName = URLDecoder.decode(lastName, "UTF-8");
            firstName = URLDecoder.decode(firstName, "UTF-8");
            birthday = URLDecoder.decode(birthday, "UTF-8");
			
            database.connect(des.decryptString(des.getServiceSecretKey(), database.getEncryptedPassword()));
			
            String SQL =
            "INSERT INTO Users"+
            " (email"+
            " ,password"+
            " ,lastName"+
            " ,firstName"+
            " ,birthday)"+
            " VALUES"+
            	" (\""+email+"\""+
            	" ,\""+password+"\""+
            	" ,\""+lastName+"\""+
            	" ,\""+firstName+"\""+
            	" ,\""+birthday+"\")";
            
            database.updateSQL(SQL);
            
            String validationCode = email;
            validationCode = URLEncoder.encode(validationCode, "UTF-8");
            validationCode = des.encryptString(des.getServiceSecretKey(), validationCode);
			
            String url = getServletContext().getInitParameter("validationAddress");
            url = url + "?accessCode="+URLEncoder.encode(des.getEmailAccessCode(), "UTF-8");
            url = url + "&email="+URLEncoder.encode(email, "UTF-8");
            url = url + "&validationCode="+URLEncoder.encode(validationCode+"", "UTF-8");
            
            mail.sendMail(des.decryptString(des.getServiceSecretKey(), mail.getEncryptedSenderMailPassword()),email, "Activation Mail", url, null);
			
            String output = URLEncoder.encode("OK","UTF-8");
            
            output = des.encryptString(session.getId(),output);
            out.print(output);
			
            database.closeConnection();
       }
    }
}
} catch (Exception ex) {}%>